package com.cnsugar.facerecgonized;

import com.cnsugar.ai.face.SeetafaceBuilder;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.*;
import java.util.Map;
import java.util.Set;


/**
 * @Author ChenZhangKun
 * @Date 2020/4/21 15:40
 */
public class SendResult implements Runnable {
    // 返回主机的ip
    private static final String ip= SeetafaceBuilder.getConfig().getProperty("send_back_ip");
    // 返回主机的端口
    private static final Integer port=Integer.parseInt(SeetafaceBuilder.getConfig().getProperty("send_back_port"));
    @Override
    public void run() {

        while (true) {
 //           synchronized (SendResult.class) {
//                if (FaceRe.listResult != null && FaceRe.listResult.size() > 0) {
//                    try {
//                        Socket socket = new Socket(ip, port);
//                        socket.setTcpNoDelay(false);
//                        //创建IO输入输出流
//                        OutputStream oStream = socket.getOutputStream();
//                        //字符输出流
//                        BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(oStream));
//                        //向服务器端发送一条消息
//                        bWriter.write(FaceRe.listResult.get(0));
//                        FaceRe.listResult.remove(0);
//                        bWriter.flush();
//                        socket.close();
////                        InetSocketAddress inetSocketAddress=new InetSocketAddress(ip,port);
////                        String content = FaceRe.listResult.get(0);
////                        DatagramPacket datagramPacket=new DatagramPacket(content.getBytes(),content.getBytes().length,inetSocketAddress);
////                        DatagramSocket datagramSocket=new DatagramSocket();
////                        datagramSocket.send(datagramPacket);
////                        FaceRe.listResult.remove(0);
//                    } catch (Exception ex) {
//                        ex.printStackTrace();
//                    }
//                }
//            }
                // 多个客户端同时连接
                synchronized (SendResult.class) {
                    if (FaceRe.mapResult != null && FaceRe.mapResult.size() > 0) {
                        Map<String, String> mapResult = FaceRe.mapResult;
                        Set<String> set = mapResult.keySet();
                        for (String s : set) {
                            if (s.contains(",")) {
                                String[] split = s.split(",");
 //                               System.out.println(split[0]+"  "+split[1]);
                                try {
                                    // 返回客户端的端口号固定为8001
                                    Socket socket = new Socket(split[0], 8001);
                                    socket.setTcpNoDelay(false);
                                    //创建IO输入输出流
                                    OutputStream oStream = socket.getOutputStream();
                                    //字符输出流
                                    BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(oStream));
                                    bWriter.write(mapResult.get(s));
                                    // 发送完毕，移除第一个
                                    mapResult.remove(s);
                                    bWriter.flush();
                                    socket.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        }
    }

